package org.gbif.metrics.cube.bucketer;

import org.gbif.api.vocabulary.Country;

import java.util.List;

import com.google.common.collect.ImmutableList;
import com.urbanairship.datacube.BucketType;
import com.urbanairship.datacube.Bucketer;
import com.urbanairship.datacube.CSerializable;
import com.urbanairship.datacube.serializables.BytesSerializable;

/**
 * Bucketer that serializes as 2 letter ISO codes.
 */
public class CountryBucketer implements Bucketer<Country> {
  // The number of bytes this buckets into (2 letter ISO codes)
  public static final int BYTES=2;
  
  @Override
  public List<BucketType> getBucketTypes() {
    return ImmutableList.of(BucketType.IDENTITY);
  }
  
  @Override
  public CSerializable bucketForWrite(Country coordinate, BucketType bucketType) {
      return bucketInternal(coordinate, bucketType);
  }

  @Override
  public CSerializable bucketForRead(Object coordinate, BucketType bucketType) {
      return bucketInternal((Country)coordinate, bucketType);
  }  

  public CSerializable bucketInternal(Country c, BucketType bucketType) {
    if (bucketType != BucketType.IDENTITY) {
      throw new IllegalArgumentException("You can only use " + CountryBucketer.class.getSimpleName()
        + " with the default identity bucketer");
    }
    return new BytesSerializable(c.getIso2LetterCode().getBytes());
  }
}
